package com.example.service.impl;

import com.example.dao.RoleDao;
import com.example.dao.RolePermDao;
import com.example.entity.Role;
import com.example.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fyxw
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao mapper;
    @Resource
    private RolePermDao rolePermDao;

    @Override
    public void addRole(Role role) {
        mapper.addRole(role);
    }

    @Override
    public Role findRoleById(int id) {
        return mapper.findRoleById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(int id) {
        if (mapper.deleteById(id) != 0) {
            rolePermDao.deleteAll(id);
        }
        return mapper.deleteById(id) != 0;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean updateRole(Role role) {
        if (mapper.findRoleById(role.getId()) != null) {
            mapper.updateRole(role);
            return true;
        }
        return false;
    }

    @Override
    public List<Role> findAll() {
        return mapper.findAll();
    }
}
